﻿using System;
using System.Collections.Generic;
using System.Linq;

namespace Zj.HttpCodeDemo.ConsoleApp.Samples
{
    internal class LinqSample
    {
        #region Fields.
        private readonly static List<Order> _orderList;
        private readonly static List<OrderItem> _orderItemList;
        private readonly static List<Product> _productList;
        private readonly static int Count = 17;
        #endregion

        #region Static ctor.
        static LinqSample()
        {
            _orderList = new List<Order>();
            for (int i = 0; i < Count; i++)
            {
                _orderList.Add(new Order());
            }//Order initialize.

            _orderItemList = new List<OrderItem>();
            for (int i = 0; i < Count; i++)
            {
                _orderItemList.Add(new OrderItem(_orderList.ElementAt(i).Id));
            }//Order item initialize.

            _productList = new List<Product>();
            for (int i = 0; i < Count; i++)
            {
                _productList.Add(new Product($"商品{i + 1}", _orderItemList.ElementAt(i).Id));
            }//Product initialize.
        } 
        #endregion

        internal static void Sample()
        {

            var list = from order in _orderList
                       join orderItem in _orderItemList
                       on order.Id equals orderItem.OrderId
                       join product in _productList
                       on orderItem.Id equals product.OrderItemId
                       select new { OrderId=order.Id,OrderItemId=orderItem.Id,Product=product.Title};
            list.ToList()
                .ForEach(o => Console.WriteLine($"{o.OrderId},{o.OrderItemId},{o.Product}"));
        }
    }

    #region Entity definition.
    /// <summary>
    /// Order concept model.
    /// </summary>
    internal class Order
        : EntityBase
    { }


    /// <summary>
    /// Order item concept model.
    /// </summary>
    internal class OrderItem
        : EntityBase
    {
        public OrderItem(string orderId)
        {
            this.OrderId = orderId;
        }
        public string OrderId { get; set; }
    }


    /// <summary>
    /// Product concept model .
    /// </summary>
    internal class Product
        : EntityBase
    {
        public Product(string title, string orderItemId)
        {
            this.Title = title;
            this.OrderItemId = orderItemId;
        }
        public string Title { get; set; }
        public string OrderItemId { get; set; }
    }

    /// <summary>
    /// Concept model base .
    /// </summary>
    internal class EntityBase
    {
        public EntityBase()
        {
            this.Id = Guid.NewGuid().ToString("N");
        }
        public string Id { get; set; }
    } 
    #endregion
}